package Code;

import java.io.File;
import java.util.Scanner;

public class QuestionNum_22_2_hxj {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入文件夹路径：");
        String s = sc.nextLine();
        File file = new File(s);

        long size = getSize(file);
        System.out.println("该文件夹的大小为" + size + "字节");
    }

    private static long getSize(File file) {
        long size = 0;
        if (file.isDirectory()) {
            // 获取file列表
            File[] files = file.listFiles();
            // 遍历列表
            for (File f : files) {
                // 判断file属性
                if (f.isFile()) {
                    size += f.length();
                } else if (f.isDirectory()) {
                    size += getSize(f);
                }
            }
        }
        return size;
    }
}